package br.mec.siep.coletor.client;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.WebServiceRef;

import com.sun.xml.internal.ws.util.ByteArrayDataSource;

import br.mec.siep.coletor.ColetorService;
import br.mec.siep.coletor.Coletor;
import br.mec.siep.coletor.SolicitarColeta;


public class ColetorClient {
        
        @WebServiceRef(wsdlLocation="http://localhost:8080/coletor/Coletor?wsdl")
        static ColetorService service = new ColetorService();

        public static void main(String[] args) {
            try {
                ColetorClient client = new ColetorClient();
                client.doTesteEnvio(args);
                client.solicitarColeta();
            } catch(Exception e) {
                e.printStackTrace();
            }
       }

       public boolean enviarArquivo(File file){
    	   try{
    		   System.out.println(" Retrieving port from the service " + service.toString());
               Coletor port = service.getColetorPort();
               System.out.println("Invocando operacao de envio");
    		   // Preparando o arquivo pra ser enviado
    		   	//DataHandler dh = new DataHandler(new FileDataSource(file));
    		   	return port.receberArquivoB(this.fileToByte(file), file.getName());
    		   	
    	   }catch(Exception ex){
    		   ex.printStackTrace();
    		   return false;
       		}
       }

       public  void doTesteEnvio(String args[])
       {
    	   File arquivo = new File("C:/arquivoCliente/testecoletor.txt");
    	   boolean resultado = enviarArquivo(arquivo);
    	   if(resultado)
    		   System.out.println("Arquivo enviado com sucesso");
    	   else
    		   System.out.println("Falha no envio do arquivo");
       	}
       
       public void solicitarColeta(){
    	   Coletor port = service.getColetorPort();    	     	   
    	   File arquivo = new File("C:/arquivoCliente/dados.txt");
    	   OutputStream out;
    	   try {
				out = new FileOutputStream(arquivo);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				baos.write(port.solicitarColeta(), 0, port.solicitarColeta().length);
				baos.writeTo(out);			
			}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();		
		}    	   
       }
       
       private byte[] fileToByte(File arquivo) throws Exception {
           FileInputStream fis = new FileInputStream(arquivo);
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           byte[] buffer = new byte[8192];
           int bytesRead = 0;
           while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
               baos.write(buffer, 0, bytesRead);
           }
           return baos.toByteArray();
       }
}

